/**
 * Created by iteny on 2016/3/3.
 */
function keyLogin(){
    if (event.keyCode==13)
        document.getElementById("loginsubmit").click();

}
//刷新验证码
function refreshs(){
    document.getElementById('code_img').src=verifycode+'?time='+Math.random();void(0);
}
$(function(){
    //不支持placeholder浏览器下对placeholder进行处理
    if (document.createElement('input').placeholder !== '') {
        $('[placeholder]').focus(function () {
            var input = $(this);
            if (input.val() == input.attr('placeholder')) {
                input.val('');
                input.removeClass('placeholder');
            }
        }).blur(function () {
            var input = $(this);
            if (input.val() == '' || input.val() == input.attr('placeholder')) {
                input.addClass('placeholder');
                input.val(input.attr('placeholder'));
            }
        }).blur().parents('form').submit(function () {
            $(this).find('[placeholder]').each(function () {
                var input = $(this);
                if (input.val() == input.attr('placeholder')) {
                    input.val('');
                }
            });
        });
    }
    $('#loginsubmit').click(function(){
        if($('#loginsubmit').attr("disabledSubmit")){
            ITENY.error('请勿重复登录','#loginsubmit');
            return false;
        }
        $('#loginsubmit').attr("disabledSubmit",true);
        var verify = $('#verifycode').val();
        var username = $('#username').val();
        var password = $('#password').val();
        if(username == "")
        {
            ITENY.error('账户名不能为空','#username');
            return false;
        }
        if(!/^[a-zA-Z][\w]{4,16}$/.test(username)) {   //如果没有匹配到，那么就错误
           ITENY.error('账户名格式错误','#username');
           return false;
        }
        if(password == "")
        {
            ITENY.error('密码不能为空','#password');
            return false;
        }
        if(!/^[a-zA-Z][\w]{7,16}$/.test(password)) {   //如果没有匹配到，那么就错误
           ITENY.error('密码格式错误','#password');
           return false;
        }
        if(verify == "")
        {
            ITENY.error('验证码不能为空','#verifycode');
            return false;
        }
        //if(!/^[a-zA-Z][w]{3,3}$/.test(verify)) {   //如果没有匹配到，那么就错误
        //    ITENY.error('验证码格式错误','#verifycode');
        //    return false;
        //}
        $.post(link,{'username':username,'password':password,'verify':verify},function(data){
            if(data==0)
            {
                ITENY.error('验证码错误','#verifycode');
                $("#verifycode").val('');
                document.getElementById('code_img').src=verifycode+'?time='+Math.random();void(0);
                $('#loginsubmit').attr("disabledSubmit",'');
            }
            else if(data==6)
            {
                ITENY.error('帐号或密码格式错误','#loginsubmit');
                $("#verifycode").val('');
                document.getElementById('code_img').src=verifycode+'?time='+Math.random();void(0);
                $('#loginsubmit').attr("disabledSubmit",'');
            }
            else if(data==4)
            {
                ITENY.error('帐号或密码错误','#loginsubmit');
                $("#verifycode").val('');
                document.getElementById('code_img').src=verifycode+'?time='+Math.random();void(0);
                $('#loginsubmit').attr("disabledSubmit",'');
            }
            else if(data==5)
            {
                ITENY.error('该账户已被封禁','#username');
                $("#verifycode").val('');
                document.getElementById('code_img').src=verifycode+'?time='+Math.random();void(0);
                $('#loginsubmit').attr("disabledSubmit",'');
            }else if(data==7)
            {
                ITENY.error('该账户不存在','#username');
                $("#verifycode").val('');
                document.getElementById('code_img').src=verifycode+'?time='+Math.random();void(0);
                $('#loginsubmit').attr("disabledSubmit",'');
            }else if(data==1){
                ITENY.success('登录成功3秒后为你跳转！','#loginsubmit');
                setTimeout(function(){window.location.href=redirect}, 3000);
            }else{
                ITENY.error('未知错误请联系管理员','#loginsubmit');
                $("#verifycode").val('');
                document.getElementById('code_img').src=verifycode+'?time='+Math.random();void(0);
                $('#loginsubmit').attr("disabledSubmit",'');
            }

        });
    });
});